package ui.module;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

import net.handler.AccepHandler;
import net.im.IMMessagePacket;
import ui.MainFrame;

public class PrivateChatModule implements Observer {
	private JTextArea displayArea;
	private JSlider fontSizeSlider;
	private JButton fontSizeBt;
	private JTextArea sendMessageArea;
	private JButton sendMessageBt;
	private JButton exitBt;

	private String ip;
	private String name;
	private StringBuffer displayBuffer = new StringBuffer();

	public JPanel uiInit(String ip, String name, String meg) {
		this.name = name;
		this.ip = ip;
		displayArea = new JTextArea();
		displayArea.setEditable(false);
		JScrollPane daScrollPane = new JScrollPane(displayArea);
		JToolBar toolBar = new JToolBar("聊天工具栏");
		toolBar.setFloatable(false);
		toolBar.addSeparator(new Dimension(15, 0));
		toolBar.add(new JLabel("设置字体大小："));
		toolBar.addSeparator(new Dimension(15, 0));
		fontSizeSlider = new JSlider();
		fontSizeSlider.setMaximum(40);
		fontSizeSlider.setValue(14);
		displayArea.setFont(new Font("微软雅黑", Font.BOLD, fontSizeSlider.getValue()));
		fontSizeSlider.setEnabled(false);
		toolBar.add(fontSizeSlider);
		toolBar.addSeparator(new Dimension(15, 0));
		fontSizeBt = new JButton("更改");
		toolBar.add(fontSizeBt);
		toolBar.addSeparator(new Dimension(15, 0));
		JPanel borderPanel = new JPanel();
		borderPanel.setLayout(new BorderLayout());
		borderPanel.add(toolBar, BorderLayout.NORTH);
		sendMessageArea = new JTextArea();
		JScrollPane smaScrollPane = new JScrollPane(sendMessageArea);
		borderPanel.add(smaScrollPane, BorderLayout.CENTER);
		JPanel flowPanel = new JPanel(new FlowLayout(2, 5, 10));
		exitBt = new JButton("关闭界面");
		sendMessageBt = new JButton("发送消息");
		flowPanel.add(exitBt);
		flowPanel.add(sendMessageBt);
		borderPanel.add(flowPanel, BorderLayout.SOUTH);

		JSplitPane SplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, daScrollPane, borderPanel);
		SplitPane.setOneTouchExpandable(true);
		SplitPane.setContinuousLayout(true);
		SplitPane.addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {

				SplitPane.setDividerLocation(0.65);
			}
		});

		JPanel chatModulePanel = new JPanel();
		chatModulePanel.setLayout(new BorderLayout());
		chatModulePanel.add(SplitPane, BorderLayout.CENTER);
		
		if(!"".equals(meg)) {
			displayBuffer.append(
					" >> " + name + " " + MainFrame.times + " :" + "\n  " + meg.trim() + "\n");
			displayArea.setText(displayBuffer.toString());
		}

		addActionListener();
		AccepHandler.getAccepHandler().addObserver(this);
		
		return chatModulePanel;
	}

	private void addActionListener() {
		exitBt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				MainFrame.jtp.remove(MainFrame.jtp.indexOfTab(name));
			}
		});

		fontSizeBt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (fontSizeBt.getText().equals("更改")) {
					fontSizeBt.setText("确定");
					fontSizeSlider.setEnabled(true);
				} else {
					displayArea.setFont(new Font("微软雅黑", Font.BOLD, fontSizeSlider.getValue()));
					fontSizeBt.setText("更改");
					fontSizeSlider.setEnabled(false);
				}
			}
		});

		sendMessageBt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (!sendMessageArea.getText().equals("")) {
					MainFrame.client.send(
							new IMMessagePacket(MainFrame.userIP, MainFrame.userName, "私聊", sendMessageArea.getText()));
					displayBuffer.append(
							" >> " + MainFrame.userName + " " + MainFrame.times + " :" + "\n  " + sendMessageArea.getText().trim() + "\n");
					displayArea.setText(displayBuffer.toString());
					sendMessageArea.setText("");
				}
			}
		});
	}

	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof AccepHandler) {
			AccepHandler pdp = (AccepHandler) o;
			IMMessagePacket ms = pdp.getData();
			if (ms.getDataType().equals("私聊")&&ip.equals(ms.getUserIP())) {
				displayBuffer.append(
						" >> " + ms.getUserName() + " " + MainFrame.times + " :" + "\n  " + ms.getData().trim() + "\n");
				displayArea.setText(displayBuffer.toString());
			}
		}
	}
}
